using System.ComponentModel;
using System.Workflow.ComponentModel;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WorkflowActions;

namespace DP.Sharepoint.Workflow
{
    public partial class CreateFolder : Activity
    {
        public static DependencyProperty __ContextProperty = DependencyProperty.Register("__Context",
                                                                                         typeof (WorkflowContext),
                                                                                         typeof (CreateFolder));

        public static DependencyProperty ContentTypeProperty = DependencyProperty.Register("ContentType",
                                                                                           typeof (string),
                                                                                           typeof (CreateFolder));

        public static DependencyProperty PathProperty = DependencyProperty.Register("Path", typeof (string),
                                                                                    typeof (CreateFolder));

        public CreateFolder()
        {
            InitializeComponent();
        }

        [Description("")]
        [Category("")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string Path
        {
            get { return ((string) (base.GetValue(PathProperty))); }
            set { base.SetValue(PathProperty, value); }
        }

        [Description("This is the description which appears in the Property Browser")]
        [Category("This is the category which will be displayed in the Property Browser")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string ContentType
        {
            get { return ((string) (base.GetValue(ContentTypeProperty))); }
            set { base.SetValue(ContentTypeProperty, value); }
        }

        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public WorkflowContext __Context
        {
            get { return ((WorkflowContext) (base.GetValue(__ContextProperty))); }
            set { base.SetValue(__ContextProperty, value); }
        }


        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            //need administrative credentials to get to Web Application Properties info
            SPSecurity.RunWithElevatedPrivileges(delegate
                                                     {
                                                         bool isSiteRelUrl = SPUrlUtility.IsUrlRelative(Path);


                                                         using (SPSite mySite = new SPSite(Path))
                                                         {
                                                             using (SPWeb myWeb = mySite.OpenWeb())
                                                             {
                                                                 SPList list = FindListFromFolderURL(myWeb, Path);

                                                                 string folderPath =
                                                                     Path.ToLower().Replace(
                                                                         myWeb.Url.ToLower() + "/" +
                                                                         list.RootFolder.Url.ToLower(), "");

                                                                 string[] folderInPath = folderPath.Split('/');

                                                                 SPFolder currFolder = list.RootFolder;

                                                                 foreach (string folder in folderInPath)
                                                                 {
                                                                     if (!string.IsNullOrEmpty(folder))
                                                                     {
                                                                         SPFolder folderTest =
                                                                             myWeb.GetFolder(currFolder.Url + "/" +
                                                                                             folder);

                                                                         if (!folderTest.Exists)
                                                                         {
                                                                             currFolder =
                                                                                 currFolder.SubFolders.Add(
                                                                                     folderTest.Url);
                                                                         }
                                                                         else
                                                                         {
                                                                             currFolder = folderTest;
                                                                         }

                                                                         SetContentType(myWeb, currFolder);
                                                                     }
                                                                 }
                                                             }
                                                         }
                                                     });

            return base.Execute(executionContext);
        }

        private void SetContentType(SPWeb web, SPFolder folder)
        {
            if (string.IsNullOrEmpty(ContentType))
                return;


            try
            {
                SPContentType cntType = web.ContentTypes[ContentType];
                SPContentType cntTypeFolder = web.ContentTypes["Folder"];

                if (cntType.Id.IsChildOf(cntTypeFolder.Id))
                {
                    folder.Item["ContentTypeId"] = cntType.Id.ToString();
                    folder.Item.SystemUpdate();
                }
            }
            catch
            {
            }
        }

        private SPList FindListFromFolderURL(SPWeb web, string url)
        {
            SPFolder folder = web.GetFolder(url);

            return web.Lists[folder.ParentListId];
        }
    }
}